<?php 
// +----------------------------------------------------------------------
// | ThinkPHP                                                             
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.      
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>                                  
// +----------------------------------------------------------------------
// $Id$

class Ipv4 extends Base
{
  private $address;
  private $netbits;

  static public function address($address='') { 
	  if(empty($address)) {
		  return self::$address; 
	  }else{
		self::$address = $address;
	  }
	}

   //--------------
  // Return the netbits
  static public function netbits($netbits='') { 
	  if(empty($netbits)) {
		  	  return self::$netbits;
	  }else{
	     self::$netbits = $netbits;
	  }
	}

   //--------------
  // Return the netmask
  static public function netmask()
  {
   return (long2ip(ip2long("255.255.255.255")
           << (32-self::$netbits)));
  }

   //--------------
  // Return the network that the address sits in
  static public function network()
  {
   return (long2ip((ip2long(self::$address))
           & (ip2long(self::netmask()))));
  }

   //--------------
  // Return the broadcast that the address sits in
  static public function broadcast()
  {
   return (long2ip(ip2long(self::$network())
           | (~(ip2long(self::netmask())))));
  }

   //--------------
  // Return the inverse mask of the netmask
  static public function inverse()
  {
   return (long2ip(~(ip2long("255.255.255.255")
           << (32-self::$netbits))));
  }

}

?>